interface Shape { // 图形 Sharp
    void draw();
}

class Rectangle implements Shape { // 矩形 Rectangle
    @Override
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}
class Square implements Shape { // 正方形 Square
    @Override
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}
class Circle implements Shape { // 圆形 Circle
    @Override
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

interface Color { // 颜色 Color
    void fill();
}

class Red implements Color { // 红 Red
    @Override
    public void fill() {
        System.out.println("Inside Red::fill() method.");
    }
}
class Green implements Color { // 绿 Green
    @Override
    public void fill() {
        System.out.println("Inside Green::fill() method.");
    }
}
class Blue implements Color { // 蓝 Blue
    @Override
    public void fill() {
        System.out.println("Inside Blue::fill() method.");
    }
}

abstract class AbstractFactory { // 抽象工厂
    public abstract Color getColor(String color);
    public abstract Shape getShape(String shape);
}

class ShapeFactory extends AbstractFactory { // 图形工厂 ShapeFactory
    @Override
    public Shape getShape(String shapeType) {
        if ( shapeType == null ) return null;

        if ( shapeType.equalsIgnoreCase("CIRCLE") ) {
            return new Circle();
        } else if ( shapeType.equalsIgnoreCase("RECTANGLE") ) {
            return new Rectangle();
        } else if ( shapeType.equalsIgnoreCase("SQUARE") ) {
            return new Square();
        }

        return null;
    }
    @Override
    public Color getColor(String color) {
        return null;
    }
}
class ColorFactory extends AbstractFactory { // 颜色工厂 ColorFactory
    @Override
    public Shape getShape(String shapeType) {
        return null;
    }
    @Override
    public Color getColor(String color) {
        if ( color == null ) return null;

        if ( color.equalsIgnoreCase("RED") ) {
            return new Red();
        } else if ( color.equalsIgnoreCase("GREEN") ) {
            return new Green();
        } else if ( color.equalsIgnoreCase("BLUE") ) {
            return new Blue();
        }

        return null;
    }
}

class FactoryProducer { // 工厂工厂
    public static AbstractFactory getFactory(String choice) {
        if ( choice.equalsIgnoreCase("SHAPE") ) {
            return new ShapeFactory();
        } else if ( choice.equalsIgnoreCase("COLOR") ) {
            return new ColorFactory();
        }

        return null;
    }
}

public class AbstractFactoryDemo {
    public static void main(String[] args) {
        AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");
 
        Shape shape1 = shapeFactory.getShape("CIRCLE");
        shape1.draw();
 
        Shape shape2 = shapeFactory.getShape("RECTANGLE");
        shape2.draw();
        
        Shape shape3 = shapeFactory.getShape("SQUARE");
        shape3.draw();
 
        AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");
 
        Color color1 = colorFactory.getColor("RED");
        color1.fill();
 
        Color color2 = colorFactory.getColor("Green");
        color2.fill();
 
        Color color3 = colorFactory.getColor("BLUE");
        color3.fill();
    }
}
